package com.daohang361.imqq.tool;

import com.daohang361.imqq.tea.Crypter;

/**
 * 用于保存复杂发送包加密数组
 * 使用发送包加密示例如下
 *  <code>Command</code> objects:
 *  <pre>
 * 	MyByteArray myByte = new MyByteArray();
 *		myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);	
 *		myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
 *		myByte.putHex("00");
 *		myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_1);
 *		myByte.putInt(myqq.getPakage());//包增长应该在接受到此包后
 *		myByte.putInt(myqq.getQq());
 *		myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
 *		myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_key);
 *		myByte.saveThis();//保存此段作为第0段
 *		myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_39);
 *		myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1);
 *		myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
 *		myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_59);
 *		myByte.saveThis();//保存此段作为第一段
 *		myByte.teaThis(1, 1, QQ2011.QQ_LOGIN_FIX_91_key);//加密第一段到第一段
 *		myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);
 *		myByte.saveThis();//保存此段作为第二段
 *		return myByte.getByteList();//返回完整的package
 *</pre>
 *@author <a href="http://www.daohang361.com">naner</a>
 */
public class MyByteSendArray implements ByteArray  {

	private static final long serialVersionUID = 3833933096057723974L;
	/**
	 * 默认支持最大分段处理为5,需要扩展的时候会自动以2*maxlength增长
	 */
	private  int maxlength=5;
	private String[] hexSs= new String[maxlength];
	/**
	 * 默认数组起始位置
	 */
	private int nowIndex=0;
	/**
	 * 临时内容保存地方
	 */
	private String hexString="";
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#putByte(byte[])
	 */
	@Override
	public void putByte(byte[] b)
	{
		String hexTemp = Hex2Bytes.bytesToHexString(b);
		hexString+=hexTemp;
	}
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#putHex(java.lang.String)
	 */
	@Override
	public void putHex(String hex)
	{
		hexString+=hex;
	}
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#putInt(int)
	 */
	@Override
	public void putInt(int value)
	{
		String temp=Integer.toHexString(value);
		if(temp.length()%2==1)
			temp="0"+temp;
		putHex(temp);
	}
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#getByteList()
	 */
	@Override
	public byte[] getByteList()
	{
		
		return Hex2Bytes.hexStringToBytes(this.getHexString());
	}
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#getHexString()
	 */
	@Override
	public String getHexString()
	{
		String hexAll="";
		for(int i =0;i<this.length()-1;i++)
			hexAll+=hexSs[i];
		return hexAll;
	}
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#saveThis()
	 */
	@Override
	public  void saveThis(){
		hexSs[nowIndex]=hexString;
		hexString="";
		nowIndex++;
		if(nowIndex==length())
			{
			  String[] newHexSs=new String[hexSs.length*2]	;
			  System.arraycopy(hexSs, 0,newHexSs, 0,  hexSs.length);
			  hexSs=newHexSs;
			}
	}
	
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#teaThis(int, int, byte[])
	 */
	@Override
	public void teaThis(int begin,int end,byte[] key)
	{
		String src="";
		for(int i=begin;i<=end;i++)
			{
				src+=hexSs[i];
				hexSs[i]="";
			}
		byte[] srcByte= Hex2Bytes.hexStringToBytes(src);
		byte[] decrypt =  new Crypter().encrypt(srcByte, key);
		hexSs[begin]=Hex2Bytes.bytesToHexString(decrypt);
	}
	/* (non-Javadoc)
	 * @see com.daohang361.imqq.tool.ByteArray#length()
	 */
	@Override
	public int length()
	{
		return nowIndex+1;
	}
}
